package com.amigo.online.provider.merchant.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.IntegerTypeHandler;
import org.springframework.stereotype.Component;

import com.amigo.online.common.core.mybatis.MyMapper;
import com.amigo.online.common.param.common.KeyValueModel;
import com.amigo.online.provider.merchant.model.entity.GoodsComment;

@Mapper
@Component
public interface GoodsCommentMapper extends MyMapper<GoodsComment> {

    List<GoodsComment> getMerchantCommentList(@Param("userId") Long userId, @Param("start") Integer start, @Param("size") Integer size);

    List<Integer> getNewFeedbackByGoodsId(@Param("goodsId") Long goodsId);

    List<Integer> getCommentCountByGoodId(@Param("id") Integer id);

    @Select("<script>"
            + " SELECT goods_id as id, count(goods_id) as value FROM goods_comment_tbl WHERE deleted = 0 GROUP BY goods_id HAVIG goods_id in "
            + " <foreach collection='goodsIds' open='(' close =')' separator=',' item ='goodsId'>"
            + " #{goodsId}"
            + " </foreach>"
            + " </script>")
    @Results({
            @Result(column = "id", id = true, property = "id"),
            @Result(column = "value", property = "value", typeHandler = IntegerTypeHandler.class)
    })
    @MapKey("id")
    Map<Long, KeyValueModel<Long, Integer>> getCommentCountByGoodIds(@Param("goodsIds") List<Long> goodsIds);

    @Select("<script>"
            + " SELECT goods_id as id, count(goods_id) as value FROM goods_comment_tbl WHERE deleted = 0 GROUP BY goods_id HAVING goods_id IN "
            + " <foreach collection='goodsIds' open='(' close =')' separator=',' item ='goodsId'>"
            + " #{goodsId}"
            + " </foreach>"
            + " </script>")
    @Results({
            @Result(column = "id", id = true, property = "id"),
            @Result(column = "value", property = "value", typeHandler = IntegerTypeHandler.class)
    }
    )
    List<KeyValueModel<Long, Integer>> listFindCommentCountByGoodIds(@Param("goodsIds") List<Long> goodsIds);

    List<GoodsComment> getCommentInfoByGoodId(@Param("id") Integer id,
                                              @Param("type") Integer type,
                                              @Param("status") Integer status,
                                              @Param("start") Integer start,
                                              @Param("size") Integer size);

    List<GoodsComment> getMerCommentListByMerId(@Param("merchantId") Long merchantId,
                                                @Param("start") Integer start,
                                                @Param("size") Integer size,
                                                @Param("status") Integer status,
                                                @Param("type") Integer type);

    @Update("update goods_comment_tbl set back_content = #{backContent},back_time = #{backTime} where id = #{id}")
    int replyComment(GoodsComment goodsComment);

}